package com.okilo.online.app.pet

import com.okilo.online.app.common.IOkiloComView
import com.okilo.online.app.common.OkiloComPet
import com.okilo.online.app.configs.OkiloConfigs
import com.okilo.online.app.configs.OkiloConfigs.addParams
import com.okilo.online.app.request.IHttpCallback
import com.okilo.online.app.lib.analyse.util.OkiloGoogleUtil
import com.okilo.online.app.pet.view.IOkiloUser
import com.okilo.online.app.tools.helper.OkiloConfigCustomerHp
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.tools.shared.OkiloSharedKey
import org.json.JSONObject

/**
 * 用户相关
 */
class OkiloUserPet(private val iView: IOkiloComView?): OkiloComPet(iView) {

    /**
     * 用户登录
     * /v1/userLoginNoCode
     */
    fun gOkiloUserLoginRequest(callback: IOkiloUser.IOkiloUserLogin){
        val mobile = callback.gOkiloUserLoginMobile()
        val adminList = OkiloConfigCustomerHp.gOkiloConfigCustomerAdminMaps()
        adminList?.let { list ->
            //是否是审核账号
            if (list.contains(mobile)){
                val accItem = list[mobile]
                //是否是广告渠道
                OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_IS_ADSOURCE,false)
                //是否是审核账号
                OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_IS_CONFIG_ACCOUNT,true)
                val accUid = accItem?.optString("accUid")
                val accTk = accItem?.optString("accTk")
                if (accUid != null && accTk != null){
                    callback.sOkiloUserLoginCallback(accUid,accTk)
                    return
                }
            }
        }
        //是否是审核账号
        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_IS_CONFIG_ACCOUNT,false)
        val httpUrl = OkiloConfigs.gOkiloUserUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("LOGIN"))
            put("mobile",mobile)//手机号
            put("code","0000")//验证码
            put("channelCode", OkiloGoogleUtil.getInstallReferrer())//渠道号
            put("subChannelCode", OkiloGoogleUtil.getInstallReferrer())//子渠道号
            put("directLoginFlag","YES")//是否直接登录
            put("notBindWalletFlag","YES")//不自动绑卡
            put("loginParam","")
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    val userId = obj.optString("userId")
                    val userToken = obj.optString("token")
                    val type = obj.optString("type")

                    //是否是渠道用户
                    val isAdSource = obj.optString("isAdSource")
                    if ("YES" == isAdSource){
                        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_IS_ADSOURCE,true)
                    }
                    callback.sOkiloUserLoginCallback(userId,userToken)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 发送WhatsApp验证码
     * /v1/sendWhatAppMessage
     */
    fun gOkiloUserVerifyMobileSendCodeRequest(callback: IOkiloUser.IOkiloVerifyMobile){
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("SEND_WHATAPP_CODE"))
            put("mobile", callback.gOkiloLoginMobile())//登录手机号
            put("submitMobile", callback.gOkiloVerifyMobile())//获取需要校验手机号码
            put("sendType",callback.gOkiloVerifyType())//发送类型: SMS、WHATSAPP
        }
        val httpUrl = OkiloConfigs.gOkiloUserUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //是否需要验证生日
                    val needSubmitBirthDay = obj.optString("needSubmitBirthDay","NO")
                    callback.sOkiloVerifyMobileCheckBirthDate(needSubmitBirthDay)

                    //当为YES时可以跳过，直接调用流程接口，根据流程步骤whatsappFlag标识走
                    val directLoginFlag = obj.optString("directLoginFlag")
                    callback.sOkiloVerifyMobileSendSuccess(directLoginFlag)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 校验手机号与验证码
     * /v1/checkWhatAppMessageCode
     */
    fun checkOkiloUserVerifyMobileRequest(submitBirthDay: String, callback: IOkiloUser.IOkiloVerifyMobile){
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("CHECK_WHATAPP_CODE"))
            put("mobile", callback.gOkiloLoginMobile())//登录手机号
            put("submitMobile", callback.gOkiloVerifyMobile())//校验手机号
            put("code",callback.gOkiloVerifyCode())//验证码
            put("checkType",callback.gOkiloVerifyType())//校验类型
            put("needSubmitBirthDay",callback.gOkiloVerifyMobileNeedSubmitBirthDay())//是否需要验证生日
        }
        if (callback.gOkiloVerifyMobileNeedSubmitBirthDay() == "YES" && submitBirthDay.isNotEmpty()){
            params.put("submitBirthDay",submitBirthDay)//出生日期
        }
        val httpUrl = OkiloConfigs.gOkiloUserUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.hideOkiloLoading()
                callback.sOkiloVerifyMobileSuccess()
            }

            override fun onRequestError(error: JSONObject) {
                if (error.optInt("errorCode") == 9999){
                    callback.sOkiloVerifyMobileFailed()
                }
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 清除用户数据 或 获取是否显示删除数据按钮
     */
    fun gOkiloUserClearDataRequest(isShowDeleteBtn: Boolean,callback: IOkiloUser.IOkiloDeleteUserData){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("CLEAR_USER_DATA"))
        }
        if (isShowDeleteBtn){
            params.put("type","GET")
        } else {
            params.put("type","DROP")
        }
        doPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                //是否显示btn
                if (isShowDeleteBtn){
                    val obj = data.optJSONObject("obj")
                    obj?.let {
                        callback.sOkiloShowDeleteUserData(obj)
                    }
                } else {
                    callback.hideOkiloLoading()
                    callback.sOkiloDeleteUserDataSuccess()
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 获取账户列表
     */
    fun gOkiloUserApplyStatusDetailsWalletListRequest(callback: IOkiloUser.IOkiloUserWallet){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("WALLET_LIST"))
        }
        doPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                val walletList = arrayListOf<JSONObject>()

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        walletList.add(array.optJSONObject(i))
                    }
                }
                callback.sOkiloUserWalletList(walletList)
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }
}